<?php
/**
 * Member.php
 * Niushop商城系统 - 团队十年电商经验汇集巨献!
 * =========================================================
 * Copy right 2015-2025 山西牛酷信息科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: http://www.niushop.com.cn
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
 * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
 * =========================================================
 * @author : niuteam
 * @date : 2015.1.17
 * @version : v1.0.0.0
 */

namespace app\admin\controller;

use data\service\Member as MemberService;
use data\service\User;
use data\service\Weixin;
use data\service\Supplier;

/**
 * 会员管理
 *
 * @author Administrator
 *
 */
class Member extends BaseController
{

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 供货商列表
     */
    public function supplierList()
    {
        $supplier = new Supplier();
        if (request()->isAjax()) {
            $page_index = request()->post("page_index", 1);
            $page_size = request()->post('page_size', PAGESIZE);
            $search_text = request()->post('search_text', '');
            $condition['supplier_name'] = array('like', '%' . $search_text . '%');
            $condition['shop_id'] = $this->instance_id;
            $list = $supplier->getSupplierList($page_index, $page_size, $condition);
            return $list;
        }
        return view($this->style . 'Member/supplierList');
    }

    /**
     * 添加供货商
     * @return multitype:unknown
     */
    public function addSupplier()
    {
        $supplier = new Supplier();
        if (request()->isAjax()) {
            $uid = request()->post('uid', 0);
            $supplier_name = request()->post('supplier_name', '');
            $linkman_name = request()->post('linkman_name', '');
            $linkman_tel = request()->post('linkman_tel', '');
            $linkman_address = request()->post('linkman_address', '');
            $desc = request()->post('desc', '');
            $res = $supplier->addSupplier($uid, $supplier_name, $linkman_name, $linkman_tel, $linkman_address, $desc);
            return AjaxReturn($res);
        }
        return view($this->style . 'Member/addSupplier');
    }

    /**
     * 修改代理商
     * @return multitype:unknown
     */
    public function updateSupplier()
    {
        $supplier = new Supplier();
        if (request()->isAjax()) {
            $supplier_id = request()->post('supplier_id', 0);
            $supplier_name = request()->post('supplier_name', '');
            $linkman_name = request()->post('linkman_name', '');
            $linkman_tel = request()->post('linkman_tel', '');
            $linkman_address = request()->post('linkman_address', '');
            $desc = request()->post('desc', '');
            $res = $supplier->updateSupplier($supplier_id, $supplier_name, $linkman_name, $linkman_tel, $linkman_address, $desc);
            return AjaxReturn($res);
        }
        $supplier_id = request()->get('supplier_id', 0);
        $info = $supplier->getSupplierInfo($supplier_id);
        if (empty($info)) {
            $this->error("当前供货商数据不存在");
        }
        $this->assign('supplier_id', $supplier_id);
        $this->assign('info', $info);
        return view($this->style . 'Member/updateSupplier');
    }

    /**
     * 删除代理商
     * @return multitype:unknown
     */
    public function deleteSupplier()
    {
        $supplier = new Supplier();
        $supplier_id_array = request()->post('supplier_id', 0);
        $res = $supplier->deleteSupplier($supplier_id_array);
        return AjaxReturn($res);
    }

    /**
     * 会员页面
     *
     * @return \think\response\View
     * @author dzl
     */
    public function memberShip()
    {
        return view($this->style . 'Member/memberShipList');
    }

    /**
     * 会员积分明细
     */
    public function pointdetail()
    {
        if (request()->isAjax()) {
            $page_index = request()->post("page_index", 1);
            $page_size = request()->post('page_size', PAGESIZE);
            $member_id = request()->post('member_id', '');
            $search_text = request()->post('search_text');
            $start_date = request()->post('start_date');
            $end_date = request()->post('end_date');
            $condition['nmar.uid'] = $member_id;
            $condition['nmar.account_type'] = 1;

            if ($start_date != '' && $end_date != '') {
                $condition["nmar.create_time"] = [
                    [
                        ">",
                        strtotime($start_date)
                    ],
                    [
                        "<",
                        strtotime($end_date)
                    ]
                ];
            } else if ($start_date != '' && $end_date = '') {
                $condition["nmar.create_time"] = [
                    [
                        ">",
                        strtotime($start_date)
                    ]
                ];
            } else if ($start_date == '' && $end_date != '') {
                $condition["nmar.create_time"] = [
                    [
                        "<",
                        strtotime($end_date)
                    ]
                ];
            }

            $condition['su.nick_name'] = [
                'like',
                '%' . $search_text . '%'
            ];

            $member = new MemberService();
            $list = $member->getPointList($page_index, $page_size, $condition, $order = '', $field = '*');
            return $list;
        }
        $member_id = $_GET['member_id'];
        $this->assign('member_id', $member_id);
        return view($this->style . 'Member/pointDetailList');
    }

    /**
     * 修改密码
     */
    public function updateMemberPassword()
    {
        $user = new User();
        $userid = request()->post('uid', '');
        $password = request()->post('user_password', '');
        $result = $user->updateUserInfoByUserid($userid, $password);
        if ($result == 1) {
            /**
             * 数据推送到队列中
             */
            $userInfo = $user->getUserInfoByUid($userid);
            redisList(2, $userInfo['user_name'], $userInfo['user_tel'], $password, 2, $this->instance_id, $userInfo['user_status']);
        }
        return AjaxReturn($result);
    }

    /**
     * 用户锁定
     */
    public function memberLock()
    {
        $uid = isset($_POST["id"]) ? $_POST["id"] : '';
        $retval = 0;
        if (!empty($uid)) {
            $member = new MemberService();
            $retval = $member->userLock($uid);
        }
        return AjaxReturn($retval);
    }

    /**
     * 查询单个 会员
     *
     * @return multitype:unknown
     */
    public function getMemberDetail()
    {
        $user = new User();
        $uid = request()->post("uid", 0);
        $info = $user->getUserInfoDetail($uid);
        return $info;
    }

    /**
     * 积分、余额调整
     */
    public function addMemberAccount()
    {
        $member = new MemberService();
        $uid = isset($_POST["id"]) ? $_POST["id"] : '';
        $type = isset($_POST["type"]) ? $_POST["type"] : '';
        $num = isset($_POST["num"]) ? $_POST["num"] : '';
        $text = isset($_POST["text"]) ? $_POST["text"] : '';
        $retval = $member->addMemberAccount(0, $type, $uid, $num, $text);
        return AjaxReturn($retval);
    }


    /**
     * 删除 会员
     *
     * @return multitype:unknown
     */
    public function deleteMember()
    {
        $member = new MemberService();
        $uid = request()->post("uid", 0);
        $res = $member->deleteMember($uid);
        if ($res == 1) {
            /**
             * 数据推送到队列中
             */
            $user = new User();
            $userInfo = $user->getUserInfoByUid($uid);
            redisList(3, $userInfo['user_name'], $userInfo['user_tel'], $userInfo['user_password'], 2, $this->instance_id, $userInfo['user_status']);
        }
        return AjaxReturn($res);
    }

    /**
     * 用户解锁
     */
    public function memberUnlock()
    {
        $uid = isset($_POST["id"]) ? $_POST["id"] : '';
        $retval = 0;
        if (!empty($uid)) {
            $member = new MemberService();
            $retval = $member->userUnlock($uid);
        }
        return AjaxReturn($retval);
    }


    /**
     * ajax  会员列表
     *
     * @return array|\data\model\unknown|\think\response\View
     * @author dzk
     */
    public function memberList()
    {
        $member = new MemberService();
        if (request()->isAjax()) {
            $page_index = request()->post('pageIndex', 1);
            $page_size = request()->post('page_size', PAGESIZE);
            $search_text = request()->post('search_text', '');
            $level_id = request()->post('levelid', -1);
            $parent_id = $this->uid;
            $condition = [
                'su.is_member' => 1,
                'nm.parent_id' => $parent_id,
                'su.nick_name|su.user_tel|su.user_email' => array(
                    'like',
                    '%' . $search_text . '%'
                ),

                'su.is_system' => 0
            ];
            if ($level_id != -1) {
                $condition['nml.level_id'] = $level_id;
            }
            $list = $member->getMemberList($page_index, $page_size, $condition, 'su.reg_time desc');
            return $list;
        } else {
            // 查询会员等级
            $list = $member->getMemberLevelList(1, 0);
            $this->assign('level_list', $list);
            return view($this->style . 'Member/memberList');
        }
    }

    /**
     * 会员添加的检查姓名
     *
     * @return bool
     * @author dzl
     */
    public function check_username()
    {
        if (request()->isAjax()) {
            // 获取数据库中的用户列表
            $username = $_GET['username'];
            $exist = false;
            $member = new MemberService();
            $user_list = $member->getMemberList();
            foreach ($user_list["data"] as $user_list2) {
                if ($user_list2["user_name"] == $username) {
                    $exist = true;
                }
            }
            return $exist;
        }
    }

    /**
     * 会员添加
     *
     * @return \multitype
     * @author dzl
     */
    public function addMember()
    {
        $member = new MemberService();
        $user_name = request()->post('username', '');
        $nick_name = request()->post('nickname', '');
        $password = request()->post('password', '');
        $member_level = request()->post('level_name', '');
        $mobile = request()->post('tel', '');
        $email = request()->post('email', '');
        $sex = request()->post('sex', '');
        $status = request()->post('status', '');
        $parent_id = $this->uid;
        $retval = $member->addMember($user_name, $password, $email, $sex, $status, $mobile, $member_level, $parent_id);
        $result = $member->updateNickNameByUid($retval, $nick_name);
        return AjaxReturn($retval);
    }

}